package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.Friend;
import com.itheima.domain.db.FriendMovement;
import com.itheima.domain.db.Movement;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author zsk
 * @create 2022-03-21-18:19
 */
@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加朋友
    @Override
    public void addFriend(Long userId, Long friendId) {
        //删除双方之间的关系
        //你删他
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);

        //他删除你
        mongoTemplate.remove(new Query(Criteria.where("friendId").is(friendId).and("userId").is(userId)), Friend.class);

        //删除对方动态
        //你删他
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)), ConstantUtil.MOVEMENT_FRIEND + userId);

        //他删你
        mongoTemplate.remove(new Query(Criteria.where("friendId").is(friendId)), ConstantUtil.MOVEMENT_FRIEND + friendId);

        //添加好友
        //你加他
        Friend friend = new Friend();
        friend.setUserId(userId);//自己id
        friend.setFriendId(friendId);//好友id
        friend.setCreated(System.currentTimeMillis());//添加时间
        //添加
        mongoTemplate.save(friend);

        //查询好友的动态
        List<Movement> movementList = mongoTemplate.find(new Query(Criteria.where("userId").is(friendId)), Movement.class);
        if (CollectionUtil.isNotEmpty(movementList)) {
            for (Movement movement : movementList) {
                //将好友动态保存到我的好友动态表中
                FriendMovement friendMovement = new FriendMovement();
                friendMovement.setCreated(movement.getCreated());//好友发布动态时的时间
                friendMovement.setPublishId(movement.getId());//好友动态id
                friendMovement.setUserId(movement.getUserId());//好友用户id

                //保存动态
                mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + userId);
            }
        }

        //他加你
        Friend friend1 = new Friend();
        friend1.setCreated(System.currentTimeMillis());//添加时间
        friend1.setUserId(friendId);//用户id
        friend1.setFriendId(userId);//好友id
        mongoTemplate.save(friend1);//添加到friend1表中

        //查询我的好友动态表
        List<Movement> movementList1 = mongoTemplate.find(new Query(Criteria.where("userId").is(userId)), Movement.class);
        if (CollectionUtil.isNotEmpty(movementList1)) {
            for (Movement movement : movementList1) {

                FriendMovement friendMovement1 = new FriendMovement();
                friendMovement1.setCreated(movement.getCreated());//你发表动态动态的时间
                friendMovement1.setPublishId(movement.getId());//你的动态id
                friendMovement1.setUserId(movement.getUserId());//你的用户id

                //保存
                mongoTemplate.save(friendMovement1, ConstantUtil.MOVEMENT_FRIEND + friendId);

            }
        }
    }

    //查看好友列表分页查询
    @Override
    public PageBeanVo findFriendByPage(Long userId, Integer pageNum, Integer pageSize) {
        //构建查询条件
        Query query = new Query(
                Criteria.where("userId").is(userId))
                .skip((pageNum - 1) * pageSize).limit(pageSize);//分页

        //查询
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        //返回分页结果
        return new PageBeanVo(pageNum,pageSize,0L,friendList);
    }
}
